{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exception"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i = 0, before exception\n",
      "this function throws an exception\n",
      "i = 1, before exception\n",
      "i = 1, after exception\n",
      "i = 2, before exception\n",
      "i = 2, after exception\n",
      "i = 3, before exception\n",
      "this function throws an exception\n",
      "i = 4, before exception\n",
      "i = 4, after exception\n"
     ]
    }
   ],
   "source": [
    "def func_throw_exception(i):\n",
    "    if i%3 == 0:\n",
    "        raise Exception('this function throws an exception')\n",
    "    \n",
    "def func():\n",
    "    for i in range(5):\n",
    "        print ('i = %d, before exception' % i)\n",
    "        try:\n",
    "            func_throw_exception(i)\n",
    "            print ('i = %d, after exception' % i)  #上面那句如果exception了，就不会运行这句话了\n",
    "        except Exception as err:\n",
    "            print (err)    # 打印报错的字符串\n",
    "        \n",
    "func()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
